package com.zivwu.mvp.pick.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zivwu.mvp.R;
import com.zivwu.utils.model.Photo;

import java.util.List;
import java.util.Map;


public class PopupDirectoryListAdapter extends BaseAdapter {
    private List<Map.Entry<String, List<Photo>>> datas;
    interface ItemClickListener {
        void onItemClick(Map.Entry<String, List<Photo>> entry);
    }


    ItemClickListener itemClickListener;

    public PopupDirectoryListAdapter(List<Map.Entry<String, List<Photo>>> datas) {
        this.datas = datas;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater mLayoutInflater = LayoutInflater.from(parent.getContext());
            convertView = mLayoutInflater.inflate(R.layout.quick_item_directory, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Map.Entry<String, List<Photo>> entry = datas.get(position);
        holder.bindData(parent.getContext(), entry);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null)
                    itemClickListener.onItemClick(entry);
            }
        });
        return convertView;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    private class ViewHolder {

        private ImageView ivCover;
        private TextView tvName;
        private TextView tvCount;

        private ViewHolder(View rootView) {
            ivCover = (ImageView) rootView.findViewById(R.id.iv_dir_cover);
            tvName = (TextView) rootView.findViewById(R.id.tv_dir_name);
            tvCount = (TextView) rootView.findViewById(R.id.tv_dir_count);
        }

        private void bindData(Context context, Map.Entry<String, List<Photo>> directory) {
            List<Photo> photos = directory.getValue();
            tvName.setText(directory.getKey());
            tvCount.setText(String.valueOf(photos.size()));
            if (photos.get(0) != null)
                Glide.with(context)
                        .load(photos.get(0).getData())
                        .into(ivCover);

        }

    }





}